#include "Login.h"
#include "../Game.h"

Login::Login()
{
	//ctor
	m_Server_lst.clear();
	m_Client_lst.clear();
}

Login::~Login()
{
	//dtor
	m_Server_lst.clear();
	m_Client_lst.clear();
}


int Login::Verification(string _user, string _pwd)
{
	int retIsSuccessd = 0; 																			// 是否成功返回标志， 0：成功， 1:帐号密码错误
	string sql = "SELECT * FROM tUser WHERE user='" + _user + "' AND pwd='" + _pwd + "';";
	int ret = Game::GetInstance().m_GameDb.Query(sql);
	if(1 != ret) 																					// 只有返回一条数据 才能通过验证
	{
		retIsSuccessd = 1; 																			// 账户密码错误
	}

	return retIsSuccessd;
}

void Login::AddServerInfo(Client* _pClient)
{
	if(_pClient == nullptr)
	{
		return;
	}

	ServerInfo Info;
	Info.m_fd = _pClient->m_fd;
	Info.m_ip = _pClient->m_ip;
	Info.m_name = _pClient->m_name;
	Info.m_port = _pClient->m_port;

	m_Server_lst.push_back(Info);
}


void Login::AddClientInfo(Client* _pClient)
{
	if(_pClient == nullptr)
	{
		return;
	}

	ClientInfo Info;
	Info.m_fd = _pClient->m_fd;
	Info.m_ip = _pClient->m_ip;
	Info.m_port = _pClient->m_port;

	m_Client_lst.push_back(Info);
}

// Fuction	: 获取某个服务器信息
ServerInfo* Login::GetServerInfo(int _fd)
{
	for(list<ServerInfo>::iterator iter = m_Server_lst.begin();iter != m_Server_lst.end(); iter++)
	{
		if(iter->m_fd == _fd)
		{
			return &(*iter);
		}
	}

	Debug::Error("Not found ServerInfo, _fd:", _fd);
	return nullptr;
}

// Fuction	: 获取某个客户端信息
ClientInfo* Login::GetClientInfo(int _fd)
{
	for(list<ClientInfo>::iterator iter = m_Client_lst.begin();iter != m_Client_lst.end(); iter++)
	{
		if(iter->m_fd == _fd)
		{
			return &(*iter);
		}
	}

	Debug::Error("Not found ClientInfo, _fd:", _fd);
	return nullptr;
}

int Login::GetServerCount()
{
	return m_Server_lst.size();
}

int Login::GetClientCount()
{
	return m_Client_lst.size();
}

list<ServerInfo>& Login::GetServerList()
{
	return m_Server_lst;
}
